package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.pojo.dto.UserLoginParam;
import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.base.response.StatusCode;
import cn.tedu._03vehicle.mapper.UserMapper;
import cn.tedu._03vehicle.pojo.dto.UserRegParam;
import cn.tedu._03vehicle.pojo.entity.User;
import cn.tedu._03vehicle.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
@Tag(name = "01-用户管理模块")
@Slf4j
@RestController
@RequestMapping("/v1/user/")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    /**
     * 1.用户注册功能
     */
    @Operation(summary = "1.用户注册功能")
    @ApiOperationSupport(order = 10)
    @PostMapping("reg")
    public JsonResult reg(@Validated @RequestBody UserRegParam userRegParam) {
        log.debug("userRegParam = " + userRegParam);
        /**
         * 1.接收客户端传入的参数
         * 2.校验数据的合法性【暂时不处理】
         * 3.校验用户名是否被占用【SELECT接口】
         */
        String username = userRegParam.getUsername();
        int count = userMapper.selectUserByUsername(username);
        //  如果查询结果 > 0，则说明用户名被占用
        if (count > 0) {
            return new JsonResult(StatusCode.USERNAME_EXISTS);
        }
        User user = new User();
        //将用户提交过来的数据放入到实体类 user中
        BeanUtils.copyProperties(userRegParam, user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        log.debug("user = " + user);
        //调用接口
        userMapper.reg(user);
        return JsonResult.ok();
    }

    /**
     * 2.用户登录功能
     */
    @Operation(summary = "2.用户登录功能")
    @ApiOperationSupport(order = 20)
    @PostMapping("login")
    public JsonResult login(@RequestBody UserLoginParam userLoginParam, HttpSession session) {
        log.debug("userLoginParam = " + userLoginParam);
        //调用接口实现
        UserVO userVO = userMapper.selectUserByUsernameAndPassword(userLoginParam);
        if(userVO == null){
            return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
        }
        //将用户信息保存到session中
        session.setAttribute("user",userVO);
        return JsonResult.ok(userVO);
    }

    /**
     * 3.获取当前登录用户信息功能
     */
    @Operation(summary = "3.获取用户信息功能")
    @ApiOperationSupport(order = 30)
    @GetMapping("currentUser")
    public JsonResult currentUser(HttpSession session) {
        //服务器内存中获取当前登录用户信息
        UserVO userVO = (UserVO) session.getAttribute("user");
        //后端情况1：{"status":2000,"message":操作成功,"data":user}
        //后端情况2：{"status":5000,"message":操作成功,"data":null}
        return JsonResult.ok(userVO);
    }

    /**
     * 4.退出登录功能
     */
    @Operation(summary = "4.退出登录功能")
    @ApiOperationSupport(order = 40)
    @GetMapping("logout")
    public JsonResult logout(HttpSession session) {
        //将session中的用户信息删除
        session.removeAttribute("user");
        return JsonResult.ok();
    }
}
